package com.lerays.spider.sougou.handler;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.lionsoul.websnail.json.JSONObject;

import com.lerays.spider.sougou.SpiderHttpServer;
import com.lerays.spider.sougou.SougouSpider;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

/**
 * default http request handler. <br />
 * {
 *  "agt": "",
 *  "uin": "",
 *  "key": "",
 *  "hook": "",
 * }
 * 
 * @author chenxin<chenxin619315@gmail.com>
 */
public class SettingRequestHandler implements HttpHandler
{
	private SpiderHttpServer httpServer = null;
	
	public SettingRequestHandler( SpiderHttpServer httpServer )
	{
		this.setHttpServer(httpServer);
	}

	@Override
	public void handle(HttpExchange e) throws IOException 
	{
		//get the request body
		StringBuilder sb = new StringBuilder();
		BufferedInputStream bis = new BufferedInputStream(e.getRequestBody());
		int chr = 0;
		while ( (chr = bis.read()) != -1 )
		{
			sb.append((char)chr);
		}
		
		//parse the incoming setting json
		JSONObject json = new JSONObject(sb.toString());
		JSONObject rets = new JSONObject();
		SougouSpider wechatSpider = httpServer.getsougouSpider();
		
		//check and set the user agent
		String agt = null;
		if ( json.has("agt") 
				&& (agt = json.getString("arg")) != null
				&& ! agt.equalsIgnoreCase(wechatSpider.getUserAgent()))
		{
			wechatSpider.setUserAgent(agt);
			rets.put("agt", true);
		}
		
		//check and set the uin
		String uin = null;
		if ( json.has("uin") 
				&& (uin = json.getString("uin")) != null
				&& ! uin.equalsIgnoreCase(wechatSpider.getUin())) 
		{
			wechatSpider.setUin(uin);
			rets.put("uin", true);
		}
		
		//check and set the key
		String key = null;
		if ( json.has("key") 
				&& (key = json.getString("key")) != null
				&& ! key.equalsIgnoreCase(wechatSpider.getKey()))
		{
			wechatSpider.setKey(key);
			rets.put("key", true);
		}
		
		//check and set the where
		String where = null;
		if ( json.has("where") 
				&& (where = json.getString("where")) != null 
				&& ! where.equalsIgnoreCase(wechatSpider.getWhere()) )
		{
			wechatSpider.setWhere(where);
			rets.put("where", true);
		}
		
		//check and set the order
		String order = null;
		if ( json.has("order") 
				&& (order = json.getString("order")) != null 
				&& ! order.equalsIgnoreCase(wechatSpider.getOrder()) )
		{
			wechatSpider.setOrder(order);
			rets.put("order", true);
		}
		
		//check and set the limit
		String limit = null;
		if ( json.has("limit") 
				&& (limit = json.getString("limit")) != null 
				&& ! limit.equalsIgnoreCase(wechatSpider.getLimit()) )
		{
			wechatSpider.setLimit(limit);
			rets.put("limit", true);
		}
		
		//check and set the interval
		int interval = 0;
		if ( json.has("interval")
				&& (interval = json.getInt("interval")) > 0
				&& interval != wechatSpider.getInterval() )
		{
			wechatSpider.setInterval(interval);
			rets.put("interval", true);
		}
		
		//check and set the callback hook
		String hook = null;
		if ( json.has("hook") 
				&& (hook = json.getString("hook")) != null )
		{
			if ( hook.equals("none") )	hook = null;
			wechatSpider.setTaskAccomplishHook(hook);
			rets.put("hook", true);
		}
		
		//send the http response
		String ret = rets.toString();
		e.sendResponseHeaders(200, ret.length());
		OutputStream os = e.getResponseBody();
		os.write(ret.getBytes());
		os.close();
		e.close();
		
		//let gc do its work
		bis.close();
		sb = null;
	}

	public SpiderHttpServer getHttpServer() 
	{
		return httpServer;
	}

	public void setHttpServer(SpiderHttpServer httpServer) 
	{
		this.httpServer = httpServer;
	}
	
}
